package com.struts.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.struts.metier.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	@Override
	public void add(User user) {
		this.getHibernateTemplate().save(user);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> getAll() { 
		return (List<User>)this.getHibernateTemplate().loadAll(User.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public User getUserByNameAndMdp(String name, String mdp) {
		try{
			DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.class);
			detachedCriteria.add(Restrictions.eq(User.PROP_NAME,name));
			detachedCriteria.add(Restrictions.eq(User.PROP_MDP, mdp)); 
			List<User> users = getHibernateTemplate().findByCriteria(detachedCriteria);
			if(users.size() == 0){
				return null;
			} 
			return users.get(0); 
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


}
